<?php
/**
 * @version		0.2.0 joomla zmluvy $
 * @package		com_zjmluvy
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author    AdrianKo		
 * @website   http://code.google.com/p/joomla-zmluvy
 *
 *
 *1.3.2012
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

class jzmluvyHelper
{
	function __construct() {

   $params = JComponentHelper::getParams ('com_jzmluvy'); 
    
    parent::__construct();
	}  
   
  function ikona ($extension,$name,$ikona) {
    
    switch ($extension)
    {

      case 'jpg':
      case 'jpeg':
      case 'jpe':
        $file = 'jpg.gif';
      break;
      case 'gif':
        $file = 'gif.gif';
      break;
      case 'png':
        $file = 'png.gif';
      break;
      case 'rtf':
        $file = 'rtf.gif';
      break;
      case 'txt':
        $file = 'txt.gif';
      break;
      case 'pdf':
        $file = 'pdf.gif';
      break;
      case 'tif':
      case 'tiff':
      case 'jfif':
      case 'jfi':
      case 'jif':
        $file = 'tif.gif';
      break;
      default:
        $file = 'default.gif';  
      
    }

    switch ($size)
    {
    case '16':
        $icon = 'icon16';
      break;
    case '48':
        $icon = 'icon48';
      break;
    }


    $image = '<img src="components/com_jzmluvy/assets/images/icon16/'.$file.'" align="bottom" width="16" height="16" title="Zobrazit dokument" alt="Zobrazit dokument" />'; 
    return $image;
  }

  function fsuma($num,$mena) {
    $output = number_format($num, 2, ',', '.').' '.$mena;
    return str_replace('.', '&nbsp;', $output);
    }
    
    //*** volitelne zobrazovanie a zadavanie formatu pre datum
  function fdatum($dat) {
  if($dat == 0) {
  $params = JComponentHelper::getParams ('com_jzmluvy');
  $output = $dat;
  } else {
  //*** pripravene na tahanie infa z xml
  //$output = date_format(date_create($dat), $this->params->get('datum'));
  $output = date_format(date_create($dat), 'd.m.Y');
  }
  return $output;
  }
  
  function format_bytes($size) {
    $units = array(' B', ' kB', ' MB', ' GB', ' TB');
    for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
    return str_replace('.',',',round($size, 0)).$units[$i];
  }

  function subor_scan ($id,$file,$filename,$extension,$link,$hits,$size) {
    $params = JComponentHelper::getParams ('com_jzmluvy');
    // echo JPATH_SITE.DS.'jzmluvy/';
    $dir = JPATH_SITE.DS.$this->params->get( 'adresar'); 
    $nazevSouboru = $id.'-'.$filename.'.'.$extension;
    if (file_exists($dir.$nazevSouboru)) {
    $filesize = filesize($dir.$nazevSouboru);
    $output = '<a target="_blank" href="'.$link.'">'.jzmluvyHelper::ikona($extension,$file,$params->get( 'ikona')).'</a>';
    if($size) {
    $output .= ' ['.jzmluvyHelper::format_bytes($filesize).']'; }
    return $output;  
	} else {
     $this->_error = JText::_( 'FA_EDIT_CHYBA_SUBOR_POSKODENY_SCAN').': '.$nazevSouboru.' ('.$id.') <br />'.JText::_( 'FA_ADMIN_KONTAKT');
	  JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	  $output .= '<img src="components/com_jzmluvy/assets/images/icon16/file_broken.png" align="middle" title="'.JText::_( 'FA_EDIT_SUBOR_POSKODENY').'" alt="'.JText::_( 'FA_EDIT_SUBOR_POSKODENY').'" />';
    return $output;
    }
    }
	
  function subor_text ($id,$file2,$filename2,$extension2,$link2,$hits2,$size2) {
    $params = JComponentHelper::getParams ('com_jzmluvy');
    // $dir = JPATH_SITE.DS.'jzmluvy/';
    $dir = JPATH_SITE.DS.$this->params->get( 'adresar'); 
    $nazevSouboru = $id.'-'.$filename2.'.'.$extension2;
    if (file_exists($dir.$nazevSouboru)) {
    $filesize = filesize($dir.$nazevSouboru);
    $output = '<a target="_blank" href="'.$link2.'">'.jzmluvyHelper::ikona($extension2,$file2,$params->get( 'ikona')).'</a>';
    if($size2) {
    $output .= ' ['.jzmluvyHelper::format_bytes($filesize).']'; }
    return $output;  
    } else {
     $this->_error = JText::_( 'FA_EDIT_CHYBA_SUBOR_POSKODENY_TEXT').': '.$id;
	  JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	  $output .= '<img src="components/com_jzmluvy/assets/images/icon16/file_broken.png" align="middle" title="'.JText::_( 'FA_EDIT_SUBOR_POSKODENY').'" alt="'.JText::_( 'FA_EDIT_SUBOR_POSKODENY').'" />';
    return $output;
    }
    }
  /*
	function fdatum($datum) {
    $datum = StrFTime("%d.%m.%Y", strtotime($datum));
    $datum = str_replace(".0", ".", $datum);
    $num = strlen($datum);
    if(substr($datum, 0, 1) == "0") $datum = substr($datum, 1, $num);

    return $datum;
  
  }*/
  
	
	function stringtodate($date) {
    $day = substr($date,0,2);
    $month = substr($date,3,2);
    $year = substr($date,6,4);
    return $year.'-'.$month.'-'.$day; 
  }
  
  function fetchButton( $type='Popup', $name = '', $text = '', $url = '', $width=640, $height=480, $top=0, $left=0 )
	{
		JHTML::_('behavior.modal');

		$text	= JText::_($text);
		$class	= jzmluvyHelper::fetchIconClass($name);
		$doTask	= jzmluvyHelper::_getCommand($name, $url, $width, $height, $top, $left);

		$html	= "<a class=\"modal\" href=\"$doTask\" rel=\"{handler: 'iframe', size: {x: $width, y: $height}}\">\n";
		$html .= "<span class=\"$class\" title=\"$text\">\n";
		$html .= "</span>\n";
		$html	.= "$text\n";
		$html	.= "</a>\n";

		return $html;
	}

 	function fetchId($name)
	{
		return $this->_parent->_name.'-'."popup-$name";
	}

  function _getCommand($name, $url, $width, $height, $top, $left)
	{
		if (substr($url, 0, 4) !== 'http') {
			$url = JURI::base().$url;
		}

		return $url;
	}
	
	function fetchIconClass($identifier)
	{
		return "icon-32-$identifier";
	}
	
	function nastavenie()
	{
		$db =& JFactory::getDBO();

		$query = 'SELECT * FROM #__jzmluvy_nastaveni WHERE id = 1';
		$db->setQuery($query);
		$nastavenie = $db->loadObject();

		return $nastavenie;
	}
	
	function accesstoedit () {
    $user = & JFactory::getUser();
    $params = JComponentHelper::getParams ('com_jzmluvy');
    
    if ($params->get('filter_groups',25) > $user->get('gid')) {
      return false;
    }
  
    return true;  
  }
	
	function adminmenu ($id, $created_by)
	{         
    $link = 'index.php?option=com_jzmluvy&view=editovatzaznam&id='.$id;
    $html = '<a href="'.$link.'"><img src="components/com_jzmluvy/assets/images/edit.gif" align="bottom" title="Editace záznamu" alt="Editace záznamu" /></a>';
//    $html .= '<div style="float: right;"><form><button style="border-width: 0px; background: none; cursor: pointer;" name="cokoliv" value="cokoliv" onclick="if (confirm(\'Are you sure you want to submit this form?\')) submit();"><img src="components/com_jzmluvy/assets/images/edit.gif" align="bottom" title="Editace záznamu" alt="Editace záznamu" /></button></form></div>';
    
    return $html;
  }
 
}


?>